package cn.itcast.service;

import cn.itcast.mapper.MenuMapper;
import cn.itcast.pojo.Menu;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenuSerciceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> findMenu() {
        List<Menu> menuList = new ArrayList<>();
        List<Menu> menus = menuMapper.findMenu();
        for (Menu menu : menus) {
            String path = menu.getPath();
            for (int i = 0; i < menus.size(); i++) {
                String i_path = String.valueOf(i);
                if(i_path.equals(path)){
                    menuList.add(menu);
                }
            }
        }

        for (Menu menu : menuList) {//遍历父节点
            List<Menu> children = new ArrayList<>();
            for (Menu menu_zi : menus) {//遍历所有节点查询当前父节点的子节点集合
                if(menu.getId() == menu_zi.getParentMenuId()){
                    children.add(menu_zi);
                }
            }
            menu.setChildren(children);
        }
        return menuList;
    }

    @Override
    public List<String> findPower(String username) {
        return menuMapper.findPower(username);
    }
}
